package com.sls.web.manager.game.service.impl;

import java.util.UUID;

import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.SnnsAuxiliaryInfo;
import com.sls.web.manager.game.querybean.SnnsAuxiliaryQuery;
import com.sls.web.manager.game.service.SnnsAuxiliaryService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 
 * @description 7996辅助信息表
 * @author Wangxuegang
 * @date 2013-4-24
 */
@Repository("snnsAuxiliaryService")
public class SnnsAuxiliaryServiceImpl extends SuperService implements
		SnnsAuxiliaryService {

	private Gson gson;

	public SnnsAuxiliaryServiceImpl() {
		gson = CommonUtil.gson();
	}

	/**
	 * 数据保存
	 * 
	 * @param snnsGoodsInfo
	 * @param userInfoVO
	 * @throws Exception
	 */
	@Override
	public void save(SnnsAuxiliaryInfo snnsAuxiliaryInfo, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_AUXI_INSERT);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		SocketDTO dto = super.send(head, snnsAuxiliaryInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param enlinkQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public Pagination<SnnsAuxiliaryInfo> querySnnsAuxiliary(
			SnnsAuxiliaryQuery snnsAuxiQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SNNS_AUXI_BY_PAGE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, snnsAuxiQuery);

		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<SnnsAuxiliaryInfo> page = gson.fromJson(body,
					new TypeToken<Pagination<SnnsAuxiliaryInfo>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
